﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class ViewDetailVendor : System.Web.UI.UserControl
    {
        public string vendorname;
        public string vendorlogo;
        public string vendordescription;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["idvendor"] != null)
                {
                    int vendorid = Convert.ToInt32(Request.QueryString["idvendor"]);
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Vendor", "*", "Vendor_ID", vendorid, "selectwhereid");
                    vendorname = dt.Rows[0]["Vendor_Name"].ToString();
                    vendorlogo = dt.Rows[0]["Vendor_LoGo"].ToString();
                    vendordescription = dt.Rows[0]["Vendor_Description"].ToString();

                    txtVendorName.Text = vendorname;
                    txtVendorDescription.Text = vendordescription;
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }      
        }

        protected void btnUpdateVendor_Click(object sender, EventArgs e)
        {
            try
            {
                int vendorid = Convert.ToInt32(Request.QueryString["idvendor"]);
                string name = txtVendorName.Text;
                string descriptionvendor = txtVendorDescription.Text;
                string filePathVendor = null;
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Vendor", "*", "Vendor_ID", vendorid, "selectwhereid");
                vendorlogo = dt.Rows[0]["Vendor_LoGo"].ToString();

                if (name != "")
                {
                    if (fulImage.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImage.FileName))
                        {
                            string link = fulImage.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Vendor/"), link, 250, fulImage.PostedFile.InputStream);
                            filePathVendor = "/Images/Image-Vendor/" + fulImage.FileName;
                            if (vendorlogo == "")
                            {
                                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                                cl[0] = new DAL.DHelpers.Column("Vendor_Name", name, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[1] = new DAL.DHelpers.Column("Vendor_LoGo", filePathVendor, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[2] = new DAL.DHelpers.Column("Vendor_Description", descriptionvendor, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                                string[] colWhere = new string[1];
                                colWhere[0] = "Vendor_ID";

                                object[] colValue = new object[1];
                                colValue[0] = vendorid;

                                DAL.DCollector.DBCollector.Instance.Update("tbl_Vendor", cl, colWhere, colValue);
                                Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
                            }
                            else
                            {
                                File.Delete(MapPath(vendorlogo));
                                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                                cl[0] = new DAL.DHelpers.Column("Vendor_Name", name, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[1] = new DAL.DHelpers.Column("Vendor_LoGo", filePathVendor, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[2] = new DAL.DHelpers.Column("Vendor_Description", descriptionvendor, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                                string[] colWhere = new string[1];
                                colWhere[0] = "Vendor_ID";

                                object[] colValue = new object[1];
                                colValue[0] = vendorid;

                                DAL.DCollector.DBCollector.Instance.Update("tbl_Vendor", cl, colWhere, colValue);
                                Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[2];
                        cl[0] = new DAL.DHelpers.Column("Vendor_Name", name, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[1] = new DAL.DHelpers.Column("Vendor_Description", descriptionvendor, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                        string[] colWhere = new string[1];
                        colWhere[0] = "Vendor_ID";

                        object[] colValue = new object[1];
                        colValue[0] = vendorid;

                        DAL.DCollector.DBCollector.Instance.Update("tbl_Vendor", cl, colWhere, colValue);
                        Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập tên danh mục...')</script>");
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }          
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            int vendorid = Convert.ToInt32(Request.QueryString["idvendor"]);
            DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_Vendor", "Vendor_ID", vendorid.ToString());
            Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
        }
    }
}